package com.smh.aiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shiminghui
 * @date 2025/6/3 16:38
 * @description: 实现一个重写用户请求的Advisor
 */
@Configuration
public class LoveAppRewriteQueryAdvisorConfig {
    @Resource
    private ChatModel dashscopeChatModel;
    @Resource
    private VectorStore loveAppVectorStore;

    @Bean
    public Advisor loveAppRewriteQueryAdvisor() {
        return RetrievalAugmentationAdvisor
                .builder()
                .documentRetriever(VectorStoreDocumentRetriever // 必须字段
                        .builder()
                        .similarityThreshold(0.5) //  设置相似度阈值
                        .vectorStore(loveAppVectorStore)
                        .topK(5)
                        .build())
                .queryTransformers(RewriteQueryTransformer
                        .builder()
                        .chatClientBuilder(ChatClient.builder(dashscopeChatModel))
                        .build())
                .build();
    }
}
